# SPDX-License-Identifier: BSD-3-Clause
# Copyright (c) 2025-2025, The OpenROAD Authors

load("@bazel_skylib//rules:common_settings.bzl", "string_flag")
load("//bazel:tcl_encode_or.bzl", "tcl_encode")
load("//bazel:tcl_wrap_cc.bzl", "tcl_wrap_cc")

package(
    features = [
        "-parse_headers",
        "-layering_check",
        # TODO(b/299593765): Fix strict ordering.
        "-libcxx_assertions",
    ],
)

exports_files([
    "LICENSE",
    "src/Design.i",
    "src/Exception.i",
])

string_flag(
    name = "platform",
    build_setting_default = "cli",
    values = [
        "cli",
    ],
)

config_setting(
    name = "platform_cli",
    flag_values = {
        ":platform": "cli",
    },
)

# TODO: once project is properly decomposed, we don't
# need these blanked dependencies in multiple places anymore.
OPENROAD_LIBRARY_DEPS = [
    ":openroad_version",
    ":ord",
    "//src/ant",
    "//src/ant:ui",
    "//src/cts",
    "//src/cts:ui",
    "//src/dbSta",
    "//src/dbSta:ui",
    "//src/dft",
    "//src/dft:ui",
    "//src/dpl",
    "//src/dpl:ui",
    "//src/drt",
    "//src/drt:ui",
    "//src/dst",
    "//src/dst:ui",
    "//src/exa",
    "//src/exa:ui",
    "//src/fin",
    "//src/gpl",
    "//src/grt",
    "//src/grt:ui",
    "//src/gui",
    "//src/ifp",
    "//src/ifp:ui",
    "//src/mpl",
    "//src/mpl:ui",
    "//src/odb",
    "//src/odb:ui",
    "//src/pad",
    "//src/par",
    "//src/par:ui",
    "//src/pdn",
    "//src/ppl",
    "//src/psm",
    "//src/rcx",
    "//src/rcx:ui",
    "//src/rsz",
    "//src/rsz:ui",
    "//src/stt",
    "//src/stt:ui",
    "//src/tap",
    "//src/tap:ui",
    "//src/upf",
    "//src/upf:ui",
    "//src/utl",
    "//src/utl:ui",
    "@edu_berkeley_abc//:abc-lib",
]

OPENROAD_COPTS = [
    "-Wno-error",
    "-Wall",
    "-Wextra",
    "-pedantic",
    "-Wno-cast-qual",  # typically from TCL swigging
    "-Wno-missing-braces",  # typically from TCL swigging
    "-Wredundant-decls",
    "-Wformat-security",
    "-Wno-sign-compare",
    "-Wno-unused-parameter",
    "-fopenmp",
]

OPENROAD_DEFINES = [
    "OPENROAD_GIT_DESCRIBE=\\\"bazel-build\\\"",
    "BUILD_TYPE=\\\"$(COMPILATION_MODE)\\\"",
    "GPU=false",
    "BUILD_PYTHON=false",
    "ABC_NAMESPACE=abc",
    "TCLRL_VERSION_STR=",
]

cc_binary(
    name = "openroad",
    srcs = [
        "src/Main.cc",
        "src/OpenRoad.cc",
        ":openroad_swig",
        ":openroad_tcl",
        ":rmp_swig",
        ":rmp_tcl",
        "//bazel:runfiles",
    ] + select({
        ":platform_cli": [],
    }),
    copts = OPENROAD_COPTS,
    features = ["-use_header_modules"],
    linkopts = select({
        ":platform_cli": [],
    }),
    malloc = "@tcmalloc//tcmalloc",
    visibility = ["//visibility:public"],
    deps = [
        ":openroad_lib_private",
        ":openroad_version",
        "//src/odb",
        "//src/rsz",
        "//src/sta:opensta_lib",
        "//src/utl",
        "@rules_cc//cc/runfiles",
        "@tk_tcl//:tcl",
    ],
)

cc_library(
    name = "openroad_lib_private",
    srcs = [
        "src/Design.cc",
        "src/Tech.cc",
        "src/Timing.cc",
    ] + glob([
        "src/rmp/src/*.h",
        "src/rmp/src/*.cpp",
    ]),
    hdrs = glob([
        "src/rmp/include/rmp/*.h",
    ]),
    copts = OPENROAD_COPTS,
    defines = OPENROAD_DEFINES + [
        "BUILD_GUI=false",
    ],
    features = ["-use_header_modules"],
    includes = [
        "include",
        "src/rmp/include",
    ],
    deps = OPENROAD_LIBRARY_DEPS,
)

cc_library(
    name = "openroad_lib",
    srcs = [
        "src/Design.cc",
        "src/OpenRoad.cc",
        "src/Tech.cc",
        "src/Timing.cc",
        ":openroad_swig",
        ":openroad_tcl",
        ":rmp_swig",
        ":rmp_tcl",
    ] + glob([
        "src/rmp/src/*.cpp",
        "src/rmp/src/*.h",
    ]),
    hdrs = glob(["src/rmp/include/rmp/*.h"]),
    copts = OPENROAD_COPTS,
    defines = OPENROAD_DEFINES + [
        "BUILD_GUI=false",
    ],
    features = ["-use_header_modules"],
    includes = [
        "include",
        "src/rmp/include",
    ],
    visibility = ["//visibility:public"],
    deps = OPENROAD_LIBRARY_DEPS,
)

cc_library(
    name = "ord",
    hdrs = [
        "include/ord/Design.h",
        "include/ord/InitOpenRoad.hh",
        "include/ord/OpenRoad.hh",
        "include/ord/Tech.h",
        "include/ord/Timing.h",
        "include/ord/Version.hh",
    ],
    includes = [
        "include",
        "include/ord",
    ],
    visibility = ["//:__subpackages__"],
)

cc_library(
    name = "openroad_version",
    hdrs = [
        ":OpenRoadVersion",
    ],
)

genrule(
    name = "OpenRoadVersion",
    srcs = [],
    outs = ["include/ord/Version.hh"],
    cmd = """echo -e '
#define OPENROAD_VERSION "HDL-HEAD"
#define OPENROAD_GIT_SHA1 "HEAD"
' > \"$@\"
    """,
)

tcl_encode(
    name = "openroad_tcl",
    srcs = ["//src/sta:tcl_util"] + [
        "src/Metrics.tcl",
        "src/OpenRoad.tcl",
    ],
    char_array_name = "ord_tcl_inits",
    namespace = "ord",
)

tcl_encode(
    name = "rmp_tcl",
    srcs = [
        "src/rmp/src/rmp.tcl",
    ],
    char_array_name = "rmp_tcl_inits",
    namespace = "rmp",
)

tcl_wrap_cc(
    name = "openroad_swig",
    srcs = [
        "src/OpenRoad.i",
        ":error_swig",
    ],
    module = "ord",
    namespace_prefix = "ord",
    root_swig_src = "src/OpenRoad.i",
    swig_includes = [
        "src",
    ],
)

tcl_wrap_cc(
    name = "rmp_swig",
    srcs = [
        "src/rmp/src/rmp.i",
        ":error_swig",
        "//src/sta:sta_swig_files",
    ],
    module = "rmp",
    namespace_prefix = "rmp",
    root_swig_src = "src/rmp/src/rmp.i",
    swig_includes = [
        "src/rmp/src",
        "src/sta",
    ],
)

filegroup(
    name = "error_swig",
    srcs = [
        "src/Exception.i",
    ],
    visibility = ["@//:__subpackages__"],
)

filegroup(
    name = "design_swig",
    srcs = [
        "src/Design.i",
    ],
)
